loadstring(exports.dgs:dgsImportFunction())()

local sw,sh = guiGetScreenSize()
local px,py = sw/1366,sh/768
local font = dgsCreateFont( ":cmta-login/OSB.ttf", 11*px )

function show(msg)
    createNotification(msg)
end
addEvent("createNotification", true)
addEventHandler("createNotification", getRootElement(), show)

local notificationQueue = {} 
local drawNotifications 
  
function createNotification(message) 
    id = 0
    if notificationQueue[id] then
        destroyNotification(id) 
    end 
     
    if type(message) ~= "string" then 
        return false 
    end 

     
    local newNotification = {} 
    newNotification.message = message 
    newNotification.textColor = {255,0,0,255} 
    newNotification.bgColor = {0,0,0,180} 
    newNotification.showTicks = 5000 
    newNotification.shownTicks = 0 
    newNotification.lastAlpha = 0 

    if #notificationQueue == 0 then 
        if not isEventHandlerAdded("onClientRender", root, drawNotifications) then
            addEventHandler("onClientRender", root, drawNotifications) 
        end
    end 
    notificationQueue[id] = newNotification 
end 
  
function destroyNotification(id) 
    if notificationQueue[id] then 
        notificationQueue[id] = nil 
     
        if #notificationQueue == 0 then 
            removeEventHandler("onClientRender", root, drawNotifications) 
        end 
    end 
end 
  
function drawNotifications() 
    if not getElementData(localPlayer,"player:uid") then return end 

    for id, notification in pairs(notificationQueue) do 
        local shownTicks, lastAlpha = notification.shownTicks, notification.lastAlpha 
        local bgColor, textColor = notification.bgColor, notification.textColor 

        -- If the nofication is still new or fading in 
        if shownTicks == 0 then
            if lastAlpha < 200 then 
                lastAlpha = lastAlpha + 3 
                if lastAlpha > 200 then 
                    lastAlpha = 200 
                end 
                 
                bgColor[4] = lastAlpha 
                textColor[4] = lastAlpha 
                notification.lastAlpha = lastAlpha 
            else 
                notification.firstTick = getTickCount() 
                notification.shownTicks = 1 
            end 
            
        elseif shownTicks >= notification.showTicks then -- Fade out or delete old notifications 
            if lastAlpha > 0 then 
                lastAlpha = lastAlpha - 6 
                if lastAlpha < 0 then 
                    lastAlpha = 0 
                end 
                 
                notification.lastAlpha = lastAlpha 
                bgColor[4] = lastAlpha 
                textColor[4] = lastAlpha 
            else 
                destroyNotification(id) 
            end 
        else -- Buisness as usual 
            notification.shownTicks = getTickCount() - notification.firstTick 
        end 
        dxDrawRectangle(0*px,740*py,1366*px,28*py,tocolor(unpack(bgColor))) 
        dxDrawText(notification.message,0*px*2,740*py*2,1366*px,28*py,tocolor(unpack(textColor)),1,font,"center","center",false,true,false,true)
    end 
end 
  

function isEventHandlerAdded( sEventName, pElementAttachedTo, func ) 
    if type( sEventName ) == 'string' and isElement( pElementAttachedTo ) and type( func ) == 'function' then 
        local aAttachedFunctions = getEventHandlers( sEventName, pElementAttachedTo ) 
        if type( aAttachedFunctions ) == 'table' and #aAttachedFunctions > 0 then 
            for i, v in ipairs( aAttachedFunctions ) do 
                if v == func then 
                    return true 
                end 
            end 
        end 
    end 
    return false 
end 